<?php
class Restaurents extends MX_Controller
{
    function __construct()
    {
        parent::__construct(); 
        $this->load->model('Restaurentmenus_model'); 
    }
    // list reataurent
    function index($keyword = '~', $row = 0)
    {
        $res = new Restaurent();
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }        
        //paging
        include('paging.php');        		
        $config['base_url']= base_url()."/restaurents/restaurents/index/".$data['key_word'].'/';         
        
        if($data['key_word']!='~')
        {
            $res->like('name', $data['key_word']);
        }
        
        $config['total_rows'] = $res->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $res = new Restaurent();
        $res->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $res->like('name', $data['key_word']);
        }
                       
        $data['list_res'] = $res->get();        
        // check login
        if(!$this->session->userdata('login'))
        {
            $data['check_login'] = false;
        }
        else 
        {
            $data['check_login'] = true;
        }
        $this->common->load_template('restaurents_index',$data);
    }
    function search($keyword = '~',$district_id = 0, $row = 0)
    {
        if ($this->input->post('district'))
        $district_id = $this->input->post('district');
        $res = new Restaurent();
        
        $data['key_word'] = urldecode($keyword);
        if($this->input->post('txtKeyWord'))
        {
            $data['key_word'] = $this->input->post('txtKeyWord');
        }        
        //paging
        include('paging.php');        		
        $config['base_url']= base_url()."/restaurents/restaurents/search/".$data['key_word'].'/'.$district_id.'/';         
        
        if($data['key_word']!='~')
        {
            $res->like('name', $data['key_word']);
        }
        // Tim nhung nha hang thuoc quan dang xet
        $res->where('district_id',$district_id);
        $config['total_rows'] = $res->count();        
        $config['cur_page'] = $row;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	
        
        $res = new Restaurent();
        $res->limit($config['per_page'], $row)
            ->order_by('id','DESC');                        
        if($data['key_word'] != '~')
        {
            $res->like('name', $data['key_word']);
        }
        $res->where('district_id',$district_id);               
        $data['list_res'] = $res->get();        
        // check login
        if(!$this->session->userdata('login'))
        {
            $data['check_login'] = false;
        }
        else 
        {
            $data['check_login'] = true;
        }
        $this->common->load_template('restaurents_search',$data);
    }
    function detail($restaurent_id = 0)//+add category
    {                          
        $term_taxonomy = new Term_taxonomy();
        $term_taxonomy->where('restaurent_id',$restaurent_id);
        $term_taxonomy->where('parent_term',0)->get();
        $restaurent_menu = $term_taxonomy->term_id;
        
        $restaurent = new Restaurent();
        $data['restaurent'] = $restaurent->get_by_id($restaurent_id);
        $data['flag'] = 0;
        // Lay ra danh sach cac thuc don con cua nha hang
        $data['list_res_menu'] = $this->Restaurentmenus_model->get_res_menu($restaurent_menu,0);        
        // check restaurent in wishlist
        $user_id = $this->session->userdata('user_id');
        $restaurent = new Restaurent();                       
        $restaurent->where('id',$restaurent_id);    
        $restaurent->include_related('term_taxonomy',array('term_id','taxonomy'))
                ->where_related('term_taxonomy', 'taxonomy', 'wishlistrestaurent')
                ->where_related('term_taxonomy', 'user_id', $user_id);                  
        $count_restaurent = $restaurent->count();  

        if ($count_restaurent>0) $data['check_like'] = true; else $data['check_like'] = false;  
        // check login
        if(!$this->session->userdata('login'))
        {
            $data['check_login'] = false;
        }
        else 
        {
            $data['check_login'] = true;
        }
        $this->common->load_template('restaurents_detail',$data);
    } 
       
    function ajax_show_detail()
    {
        $id = $this->input->post('id');
        $product = new Product();
        $product->get_by_id($id);
        $data = ''; 
        $data.='<div class="view_food_ajax">';
        $data.='<div class="avatar_ajax">';
        $data.='<a href="#"><img alt="'.$this->common->get_lang($product->name).'" src="'.$product->image.'" /></a>';
        $data.='</div>';
        $data.='<div class="food_name_ajax">';            			
        $data.='<a href="#"><span >'.$this->common->get_lang($product->name).'</span></a>';            			
        $data.='</div>';                       
        $data.='<div class="des_food_ajax">';
        $data.='<span>'.$this->common->get_lang($product->description).'</span>';
        //$data.='<br/><div class="time_foods">';            						
        //$data.='Mở cửa : 08h:30 - 20h:00';
        //$data.='</div>';
        $data.='</div>' ;
        $data.='<div class="price_food_ajax">';
        $data.='<span>Giá :  '.$product->price.' VNĐ</span>';         
        $data.='</div></div>';
        echo $data;
    }
    
}
?>